<?php

/**
 * 资讯类型管理
 * @author renpper
 * @date 2014-12-29
 */

namespace Admin\Controller;
class NewsTypeController extends BaseController {

    private $news_type_model;
    
    public function _initialize(){
		$this->news_type_model = getInstance('Admin\Model\NewsTypeModel');
	}
	
	/**
	 * 资讯类型列表页
	 */
	public function index(){
		$page = $this->view->page(count($this->news_type_model->where('form')->select()));
		$news_type = $this->news_type_model->where('form')->order($page['orderFieldStr'])->limit("{$page['limit']},{$page['numPerPage']}")->select();
		$this->assign('news_type', $news_type);
		$this->assign('page', $page);
		$this->display('Admin/View/NewsType/index.php');
	}
	
	/**
	 * 添加资讯类型
	 */
	public function add(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$datas['create_time'] = date('Y-m-d H:i:s');
			if($this->news_type_model->add($datas)){
				$this->ajaxReturn(200, '添加成功','','closeCurrent');
			}else{
				$this->ajaxReturn(300, '添加失败');
			}
		}else{
			$this->display('Admin/View/NewsType/add.php');
		}
	}
	
	/**
	 * 修改资讯类型
	 */
	public function update(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$id = $datas['id'];
			unset($datas['id']);
			if($this->news_type_model->where('id='.$id)->update($datas)){
				$this->ajaxReturn(200, '修改成功');
			}else{
				$this->ajaxReturn(300, '修改失败');
			}
		}else{
			$news_type_id = (int)$_REQUEST['id'];
			$news_type_info = $this->news_type_model->where("id=$news_type_id")->find();
			$this->assign('news_type_info', $news_type_info);
			$this->display('Admin/View/NewsType/update.php');
		}
	}
	
	/**
	 * 删除资讯类型
	 */
	public function delete(){
		$ids = $_REQUEST['ids'];
		if(!empty($ids) && $this->news_type_model->where("id IN($ids)")->delete()){
			$this->ajaxReturn(200, '删除成功');
		}else{
			$this->ajaxReturn(300, '删除失败');
		}
	}
    
}
?>